package com.enqualcomm.kids.view.heartrate;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.enqualcomm.kids.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sunzhigang on 2016/7/18.
 */
public class HeartRateBar extends RelativeLayout {
    private ImageView bar_thumber;
    private HeartRateSetBarView rateSetBarView;
    private List<Float> mThumbContainerXPosition = new ArrayList<>();
    private float singleLength;

    public HeartRateBar(Context context) {
        super(context);
        initView(context);
    }

    public HeartRateBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HeartRateBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.heart_rate_bar, this);
        bar_thumber = (ImageView) findViewById(R.id.bar_thumber);
        rateSetBarView = (HeartRateSetBarView) findViewById(R.id.rateSetBarView);
        rateSetBarView.setOnDrawReadyListener(new HeartRateSetBarView.onDrawReadyListener() {
            @Override
            public void onReady(float startX, float endX, float singleLength, int nums) {
                calculate(startX, endX, singleLength, nums);
                if (selectedPosition!=-1){
                    setThumberPosition(selectedPosition);
                    selectedPosition = -1;
                }
            }
        });
    }

    private void calculate(float startX, float endX, float singleLength, int nums) {
        this.singleLength = singleLength;
        mThumbContainerXPosition.add(startX);
        for (int i = 1; i < nums - 1; i++) {
            mThumbContainerXPosition.add(startX + (i * singleLength));
        }
        mThumbContainerXPosition.add(endX);
        bar_thumber.setTranslationX(mThumbContainerXPosition.get(0));
    }

    private float mTouchLeftX; //记录移动过程中的位置

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                mTouchLeftX = ev.getX();
                if (mTouchLeftX <= mThumbContainerXPosition.get(0)) {
                    mTouchLeftX = mThumbContainerXPosition.get(0);
                } else if (mTouchLeftX >= mThumbContainerXPosition.get(mThumbContainerXPosition.size() - 1)) {
                    mTouchLeftX = mThumbContainerXPosition.get(mThumbContainerXPosition.size() - 1);
                }
                bar_thumber.setTranslationX(mTouchLeftX);
                break;
            case MotionEvent.ACTION_UP:
                mTouchLeftX = ev.getX();
                if (mTouchLeftX <= mThumbContainerXPosition.get(0)) {
                    mTouchLeftX = mThumbContainerXPosition.get(0);
                } else if (mTouchLeftX >= mThumbContainerXPosition.get(mThumbContainerXPosition.size() - 1)) {
                    mTouchLeftX = mThumbContainerXPosition.get(mThumbContainerXPosition.size() - 1);
                } else {
                    mTouchLeftX = calculatePositionX(ev.getX());
                }
                bar_thumber.setTranslationX(mTouchLeftX);
                if (listener != null) {
                    if (mTouchLeftX ==mThumbContainerXPosition.get(mThumbContainerXPosition.size()-1) ){
                        listener.onChange(mThumbContainerXPosition.size()-1);
                    }else {
                        listener.onChange(positon);
                    }

                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private int positon = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    private float calculatePositionX(float X) {
        boolean isNegative = false;
        float lastX = 0;
        if (X < 0) {
            X = -X;
            isNegative = true;
        }
        for (int i = 0; i < mThumbContainerXPosition.size() - 1; i++) {
            if (X > mThumbContainerXPosition.get(i) && X < mThumbContainerXPosition.get(i + 1)) {
                if (X < (mThumbContainerXPosition.get(i) + (singleLength / 2.0))) {
                    lastX = mThumbContainerXPosition.get(i);
                    positon = i;
                } else {
                    lastX = mThumbContainerXPosition.get(i + 1);
                    positon = i + 1;
                }
                break;
            }
        }
        return isNegative ? -lastX : lastX;
    }

    RateOnchangeListener listener;

    public void setOnRateOnchangeListener(RateOnchangeListener listener) {
        this.listener = listener;
    }

    public interface RateOnchangeListener {
        void onChange(int position);
    }

    private int selectedPosition = -1;
    public void setThumberPosition(int position){
        if (mThumbContainerXPosition.size()>position){
            mTouchLeftX = mThumbContainerXPosition.get(position);
            bar_thumber.setTranslationX(mTouchLeftX);
        }else {
            selectedPosition = position;
        }
    }
}
